package com.smday.stack;


/**
 * @author Summerday
 */
public class ArrayStackDemo {

    public static void main(String[] args) {

        ArrayStack stack = new ArrayStack(5);

        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);

        stack.show();
        stack.pop();

        stack.show();

    }
}


//用数组实现栈
class ArrayStack{

    private int maxSize;
    private int top;
    private int[] stack;

    ArrayStack(int maxSize){
        //栈的大小
        this.maxSize = maxSize;
        //数组
        stack = new int[maxSize];
        //栈顶,初始化-1
        top = -1;
    }

    public boolean isFull(){
        return top == maxSize-1;
    }

    public boolean isEmpty(){
        return top == -1;
    }

    public void push(int data){
        if(isFull()){
            System.out.println("栈已满!");
            return;
        }
        stack[++top] = data;
    }

    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("栈为空!");
        }
        int value = stack[top--];
        System.out.printf("pop -->  %d\n",value);
        return value;

    }

    public void show(){
        if(isEmpty()){
            System.out.println("栈空,没有数据");
            return;
        }
        System.out.println("=======STACK=======");
        System.out.println();
        System.out.printf("top -->  stack[%d]=%d\n",top,stack[top]);
        for(int i = top-1;i>0;i --){
            System.out.printf("         stack[%d]=%d\n",i,stack[i]);
        }
        System.out.printf("bot -->  stack[%d]=%d\n",0,stack[0]);

    }

}
